<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="服务名" prop="serviceName">
        <el-input v-model="queryParams.serviceName" placeholder="请输入服务名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceCategory">
        <el-input v-model="queryParams.serviceCategory" placeholder="请输入服务类型" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="服务内容" prop="serviceDetails">
        <el-input v-model="queryParams.serviceDetails" placeholder="请输入服务内容" clearable @keyup.enter="handleQuery" />
      </el-form-item>
<!--      <el-form-item label="服务图片" prop="servicePic">-->
<!--        <el-input v-model="queryParams.servicePic" placeholder="请输入服务图片" clearable @keyup.enter="handleQuery" />-->
<!--      </el-form-item>-->
      <el-form-item label="服务时长" prop="serviceTime">
        <el-input v-model="queryParams.serviceTime" placeholder="请输入服务时长" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="服务价格" prop="servicePrice">
        <el-input v-model="queryParams.servicePrice" placeholder="请输入服务价格" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="销量" prop="sold">
        <el-input v-model="queryParams.sold" placeholder="请输入销量" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入店铺名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="上架时间" prop="launchTime">
        <el-date-picker clearable v-model="queryParams.launchTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择上架时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下架时间" prop="downTime">
        <el-date-picker clearable v-model="queryParams.downTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择下架时间">
        </el-date-picker>
      </el-form-item>
      <el-button type="primary" icon="search" size="default" @click="handleQuery">搜索</el-button>
      <el-button icon="refresh" size="default" @click="resetQuery">重置</el-button>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">

        <el-button type="primary" plain icon="plus" size="mini" @click="handleAdd"
          v-hasPermi="['shop:service:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['shop:service:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['shop:service:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" size="mini" @click="handleExport"
          v-hasPermi="['shop:service:export']">导出</el-button>

      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务ID" align="center" prop="serviceId" />
      <el-table-column label="服务名" align="center" prop="serviceName" />
      <el-table-column label="服务类型" align="center" prop="serviceCategory" />
      <el-table-column label="服务内容" align="center" prop="serviceDetails" />
      <el-table-column label="服务图片" align="center" prop="servicePic" >
      <template #default="scope">
        <img v-if="scope.row.servicePic" :src="scope.row.servicePic" style="width: 100px; height: 100px;" />
      </template>
       </el-table-column>
      <el-table-column label="服务时长" align="center" prop="serviceTime" />
      <el-table-column label="服务价格" align="center" prop="servicePrice" />
      <el-table-column label="销量" align="center" prop="sold" />
      <el-table-column label="店铺" align="center" prop="shopName" />
      <el-table-column label="上架时间" align="center" prop="launchTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.launchTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下架时间" align="center" prop="downTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.downTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务状态" align="center" prop="serviceStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:service:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['shop:service:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改服务信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="serviceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务名" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceCategory">
          <el-input v-model="form.serviceCategory" placeholder="请输入服务类型" />
        </el-form-item>
        <el-form-item label="服务内容" prop="serviceDetails">
          <el-input v-model="form.serviceDetails" placeholder="请输入服务内容" />
        </el-form-item>
        <el-form-item label="店员图片" prop="servicePic">
          <image-upload v-model="form.servicePic" @success="handleSuccess" @error="handleError" />
        </el-form-item>
        <el-form-item label="服务时长" prop="serviceTime">
          <el-input v-model="form.serviceTime" placeholder="请输入服务时长" />
        </el-form-item>
        <el-form-item label="服务价格" prop="servicePrice">
          <el-input v-model="form.servicePrice" placeholder="请输入服务价格" />
        </el-form-item>
        <el-form-item label="销量" prop="sold">
          <el-input v-model="form.sold" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="店铺" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名" />
        </el-form-item>
        <el-form-item label="上架时间" prop="launchTime">
          <el-date-picker clearable v-model="form.launchTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择上架时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下架时间" prop="downTime">
          <el-date-picker clearable v-model="form.downTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择下架时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务状态" prop="serviceStatus">
          <el-input v-model="form.serviceStatus" placeholder="请输入逻辑删除，1-已删除，2-未删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Service">
import { listService, getService, delService, addService, updateService } from "@/api/shop/service";
import { listShop } from "@/api/shop/shop";
import useUserStore from '@/store/modules/user';
const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const serviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serviceName: null,
    serviceCategory: null,
    serviceDetails: null,
    servicePic: null,
    serviceTime: null,
    servicePrice: null,
    sold: null,
    shopName: null,
    launchTime: null,
    downTime: null,
    serviceStatus: null
  },
  queryShopParams: {
    pageNum: 1,
    pageSize: 10,
    shopName: null,
    shopDesc: null,
    shopNumber: null,
    shopStatus: null,
    shopTimeStart: null,
    shopPic: null,
    shopAddress: null,
    phoneNumber: null,
    employeeName: null,
    bossName: null
  },
  rules: {
    serviceName: [
      { required: true, message: "服务名不能为空", trigger: "blur" }
    ],
    serviceCategory: [
      { required: true, message: "服务类型不能为空", trigger: "blur" }
    ],
    serviceDetails: [
      { required: true, message: "服务内容不能为空", trigger: "blur" }
    ],
    servicePic: [
      { required: true, message: "服务图片不能为空", trigger: "blur" }
    ],
    serviceTime: [
      { required: true, message: "服务时长不能为空", trigger: "blur" }
    ],
    servicePrice: [
      { required: true, message: "服务价格不能为空", trigger: "blur" }
    ],
    sold: [
      { required: true, message: "销量不能为空", trigger: "blur" }
    ],
    shopName: [
      { required: true, message: "店铺名不能为空", trigger: "blur" }
    ],
    launchTime: [
      { required: true, message: "上架时间不能为空", trigger: "blur" }
    ],
    downTime: [
      { required: true, message: "下架时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
    isDeleted: [
      { required: true, message: "逻辑删除，1-已删除，2-未删除不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams,queryShopParams, form, rules } = toRefs(data);

/** 查询服务信息列表 */
function getList() {
  loading.value = true;
  if (userStore.roles[0] == "admin") {
    listService(queryParams.value).then(response => {
      serviceList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  } else if (userStore.roles[0] == "shopManager") {
    // 如果角色为店铺管理员
    queryShopParams.value.employeeName = userStore.name;
    // 查询出管理员管理的店铺
    listShop(queryShopParams.value).then(response => {
      queryParams.value.shopName = response.rows[0].shopName;
      total.value = response.total;
      loading.value = false;
      // 查询出该店铺的服务
      listService(queryParams.value).then(response => {
        serviceList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
    }).catch(error => {
      console.log(error);
    });
  } else if (userStore.roles[0] == "boss") {
    queryShopParams.value.bossName = userStore.name;
    // 查询旗下店铺
    listShop(queryShopParams.value).then(response => {
      // 根据旗下店铺查询出所有服务
      for (const shop in response.rows) {
        queryParams.value.shopName = response.rows[shop].shopName;
        listService(queryParams.value).then(response => {
          // 遍历查询结果封装入服务查询列表
          response.rows.forEach(element => {
           serviceList.value.push(element);
          });
          total.value = response.total;
          loading.value = false;
        });
      }
    }).catch(error => {
      console.log(error);
    });
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    serviceId: null,
    serviceName: null,
    serviceCategory: null,
    serviceDetails: null,
    servicePic: null,
    serviceTime: null,
    servicePrice: null,
    sold: null,
    shopId: null,
    launchTime: null,
    downTime: null,
    updateTime: null,
    serviceStatus: null
  };
  proxy.resetForm("serviceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.serviceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加服务信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _serviceId = row.serviceId || ids.value
  getService(_serviceId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改服务信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["serviceRef"].validate(valid => {
    if (valid) {
      if (form.value.serviceId != null) {
        updateService(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addService(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _serviceIds = row.serviceId || ids.value;
  proxy.$modal.confirm('是否确认删除服务信息编号为"' + _serviceIds + '"的数据项？').then(function () {
    return delService(_serviceIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shop/service/export', {
    ...queryParams.value
  }, `service_${new Date().getTime()}.xlsx`)
}

getList();
</script>
